package com.ruoyi.blindBoxManage.service.impl;

import com.ruoyi.blindBoxManage.domain.BlindBoxProduct;
import com.ruoyi.blindBoxManage.domain.BlindBoxPublicProduct;
import com.ruoyi.blindBoxManage.domain.BlindProduct;
import com.ruoyi.blindBoxManage.domain.Box;
import com.ruoyi.blindBoxManage.mapper.*;
import com.ruoyi.blindBoxManage.service.IBlindProductService;
import com.ruoyi.common.utils.DateUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 盲盒商品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-22
 */
@Slf4j
@Service
public class BlindProductServiceImpl implements IBlindProductService {
    @Autowired
    private BlindProductMapper blindProductMapper;
    @Resource
    private BlindBoxPublicProductMapper blindBoxPublicProductMapper;
    @Resource
    private BoxMapper boxMapper;
    @Resource
    private BlindBoxMapper blindBoxMapper;
    @Resource
    private BlindBoxProductMapper blindBoxProductMapper;

    /**
     * 查询盲盒商品
     *
     * @param id 盲盒商品主键
     * @return 盲盒商品
     */
    @Override
    public BlindProduct selectBlindProductById(Long id) {
        return blindProductMapper.selectBlindProductById(id);
    }

    /**
     * 查询盲盒商品列表
     *
     * @param blindProduct 盲盒商品
     * @return 盲盒商品
     */
    @Override
    public List<BlindProduct> selectBlindProductList(BlindProduct blindProduct) {
        return blindProductMapper.selectBlindProductList(blindProduct);
    }

    /**
     * 新增盲盒商品
     *
     * @param blindProduct 盲盒商品
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int insertBlindProduct(BlindProduct blindProduct) {
        if (blindProduct.getPublicProductId() != null) {
            BlindBoxPublicProduct blindBoxPublicProduct = blindBoxPublicProductMapper.selectBlindBoxPublicProductById(blindProduct.getPublicProductId());
            if (ObjectUtils.isEmpty(blindBoxPublicProduct)) {
                throw new RuntimeException("请选择商品");
            }
            blindProduct.setExcessCount(blindProduct.getTotalCount());
            blindProduct.setClassId(blindBoxPublicProduct.getClassId());
            blindProduct.setName(blindBoxPublicProduct.getName());
            blindProduct.setPicture(blindBoxPublicProduct.getPicture());
            blindProduct.setSeriesName(blindBoxPublicProduct.getSeriesName());
            blindProduct.setPrice(blindBoxPublicProduct.getPrice());
            blindProduct.setIsSplit(blindBoxPublicProduct.getIsSplit());
            blindProduct.setSplitCount(blindBoxPublicProduct.getSplitCount());
            blindProduct.setIsDelivery(blindBoxPublicProduct.getIsDelivery());
            blindProduct.setFreight(blindBoxPublicProduct.getFreight());
        }
        blindProduct.setCreateTime(DateUtils.getNowDate());
        //查询该盲盒是否已经有箱子
        List<Box> boxes = boxMapper.selectBoxListByBlindBoxId(blindProduct.getBlindBoxId());
        if (!CollectionUtils.isEmpty(boxes)) {
            for (Box box : boxes) {
                BlindBoxProduct insertBlindBoxProduct = new BlindBoxProduct();
                BeanUtils.copyProperties(blindProduct, insertBlindBoxProduct);
                insertBlindBoxProduct.setBoxId(box.getId());
                blindBoxProductMapper.insertBlindBoxProduct(insertBlindBoxProduct);
            }
        }
        return blindProductMapper.insertBlindProduct(blindProduct);
    }

    /**
     * 修改盲盒商品
     *
     * @param blindProduct 盲盒商品
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int updateBlindProduct(BlindProduct blindProduct) {
        BlindProduct blindProduct1 = blindProductMapper.selectBlindProductById(blindProduct.getId());
        //先判断有没有切换公共商品
        //&& !blindProduct.getPublicProductId().equals(blindProduct1.getPublicProductId())
        if (blindProduct.getPublicProductId() != null ) {
            BlindBoxPublicProduct blindBoxPublicProduct = blindBoxPublicProductMapper.selectBlindBoxPublicProductById(blindProduct.getPublicProductId());
            if (ObjectUtils.isEmpty(blindBoxPublicProduct)) {
                throw new RuntimeException("请选择商品");
            }
            blindProduct.setClassId(blindBoxPublicProduct.getClassId());
            blindProduct.setName(blindBoxPublicProduct.getName());
            blindProduct.setPicture(blindBoxPublicProduct.getPicture());
            blindProduct.setSeriesName(blindBoxPublicProduct.getSeriesName());
            blindProduct.setPrice(blindBoxPublicProduct.getPrice());
            blindProduct.setIsSplit(blindBoxPublicProduct.getIsSplit());
            blindProduct.setSplitCount(blindBoxPublicProduct.getSplitCount());
            blindProduct.setIsDelivery(blindBoxPublicProduct.getIsDelivery());
            blindProduct.setFreight(blindBoxPublicProduct.getFreight());
        }

        //查询盲盒中所有绑定的商品
        List<BlindBoxProduct> blindBoxProducts = blindBoxProductMapper.selectBlindList(blindProduct1.getPublicProductId(), blindProduct.getBlindBoxId());
        if (!CollectionUtils.isEmpty(blindBoxProducts)) {
            for (BlindBoxProduct boxProduct : blindBoxProducts) {
                Long id = boxProduct.getId();
                //先判断有没有切换公共商品
                //&& !blindProduct.getPublicProductId().equals(boxProduct.getPublicProductId())
                if (blindProduct.getPublicProductId() != null ) {
                    BlindBoxPublicProduct blindBoxPublicProduct = blindBoxPublicProductMapper.selectBlindBoxPublicProductById(blindProduct.getPublicProductId());
                    if (ObjectUtils.isEmpty(blindBoxPublicProduct)) {
                        throw new RuntimeException("请选择商品");
                    }
                    blindProduct.setClassId(blindBoxPublicProduct.getClassId());
                    blindProduct.setName(blindBoxPublicProduct.getName());
                    blindProduct.setPicture(blindBoxPublicProduct.getPicture());
                    blindProduct.setSeriesName(blindBoxPublicProduct.getSeriesName());
                    blindProduct.setPrice(blindBoxPublicProduct.getPrice());
                    blindProduct.setIsSplit(blindBoxPublicProduct.getIsSplit());
                    blindProduct.setSplitCount(blindBoxPublicProduct.getSplitCount());
                    blindProduct.setIsDelivery(blindBoxPublicProduct.getIsDelivery());
                    blindProduct.setFreight(blindBoxPublicProduct.getFreight());
                }
                blindProduct.setUpdateTime(DateUtils.getNowDate());
                BeanUtils.copyProperties(blindProduct, boxProduct);
                boxProduct.setId(id);
                boxProduct.setBoxId(boxProduct.getBoxId());
                boxProduct.setId(boxProduct.getId());
                blindBoxProductMapper.updateBlindBoxProduct(boxProduct);
            }
        }
        blindProduct.setUpdateTime(DateUtils.getNowDate());
        return blindProductMapper.updateBlindProduct(blindProduct);
    }

    /**
     * 批量删除盲盒商品
     *
     * @param ids 需要删除的盲盒商品主键
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int deleteBlindProductByIds(Long[] ids) {
        for (Long id : ids) {
            BlindProduct blindProduct = blindProductMapper.selectBlindProductById(id);
            List<BlindBoxProduct> blindBoxProducts = blindBoxProductMapper.selectBlindList(blindProduct.getPublicProductId(), blindProduct.getBlindBoxId());
            //查询盲盒箱子中是否有绑定的商品
            if (!CollectionUtils.isEmpty(blindBoxProducts)) {
                throw new RuntimeException("删除失败,商品:" + blindProduct.getName() + " 在盲盒中有绑定");
            }
        }
        return blindProductMapper.deleteBlindProductByIds(ids);
    }

    /**
     * 删除盲盒商品信息
     *
     * @param id 盲盒商品主键
     * @return 结果
     */
    @Override
    public int deleteBlindProductById(Long id) {
        return blindProductMapper.deleteBlindProductById(id);
    }
}
